<template>
  <div>
    <a-form layout="horizontal" :form="form">
      <a-form-item
        label="付款账户"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-input
          placeholder="请输入付款账户"
          v-decorator="[
            'payAccount',
            {
              initialValue: step.payAccount,
              rules: [{ required: true, min: 5, message: '请输入付款账户' }]
            }
          ]"
        >
        </a-input>
      </a-form-item>
      <a-form-item
        label="收款账户"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <!-- 自定义的表单组件也可搭配form使用, 遵循 valuePropName、 trigger 的参数与该组件内部相同即可-->
        <receiver-account
          v-decorator="[
            'receiverAccount',
            {
              initialValue: step.receiverAccount,
              valuePropName: 'receiverAccount',
              trigger: 'change',
              rules: [{ validator: checkReceiverAccount, required: true }]
            }
          ]"
        >
        </receiver-account>
      </a-form-item>
      <a-form-item
        :label-col="formItemOffsetLayout.labelCol"
        :wrapper-col="formItemOffsetLayout.wrapperCol"
      >
        <a-button type="primary" @click="handleSubmit" style="width: 100%"
          >下一步</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import ReceiverAccount from "./../../../components/ReceiverAccount";
import { mapState, mapActions } from "vuex";

export default {
  name: "Info",
  components: {
    ReceiverAccount
  },
  data() {
    this.form = this.$form.createForm(this, { name: "stepOne" });
    return {
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 10 }
      },
      formItemOffsetLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 4, offset: 4 }
      }
    };
  },
  computed: {
    ...mapState("form", {
      step: state => state.step
    })
  },
  methods: {
    ...mapActions("form", ["submitStepForm"]),
    handleSubmit() {
      let _this = this;
      this.form.validateFields(function(error, values) {
        if (!error) {
          // 第一步完成后不发送请求，因此可以直接commit保存数据到vuex中，不用先dispatch了，接着执行第二步
          const { $store, $router } = _this;
          $store.commit({
            type: "form/saveStepFormData",
            params: values
          });
          $router.push({ name: "confirm" });
        }
      });
    },
    //自定义检测 必须调用callback回调，告知异步检测结束
    checkReceiverAccount(rules, value, callback) {
      if (value && value.number) {
        if (value.number.length <= 5) {
          callback("收款账户格式错误");
        }
        callback();
      } else {
        callback("请输入收款账户");
      }
    }
  }
};
</script>

<style lang="less"></style>
